package com.asen.commons.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/***
 * 日期工具类
 */
@Slf4j
public final class DateUtil {

    private DateUtil() {

    }

    public static final String[] ALL_FORMATS = new String[]{"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm", "yyyy/MM/dd", "yyyyMMdd HH:mm:ss.SSS", "yyyyMMdd HH:mm:ss", "yyyyMMdd HH:mm", "yyyyMMdd HH:mm", "yyyyMMdd"};

    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /***
     * yyyy-MM-dd
     */
    public static final String FORMAT1 = "yyyy-MM-dd";

    /***
     * yyyy.MM.dd
     */
    public static final String FORMAT2 = "yyyy.MM.dd";

    /***
     * yyyy/MM/dd
     */
    public static final String FORMAT3 = "yyyy/MM/dd";

    /***
     * yyyy-MM-dd HH:mm
     */
    public static final String FORMAT4 = "yyyy-MM-dd HH:mm";

    /***
     * yyyy.MM.dd HH:mm
     */
    public static final String FORMAT5 = "yyyy.MM.dd HH:mm";

    /***
     * yyyy/MM/dd HH:mm
     */
    public static final String FORMAT6 = "yyyy/MM/dd HH:mm";

    /***
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String FORMAT7 = "yyyy-MM-dd HH:mm:ss";

    /***
     * YYYY-MM-dd HH-mm-ss
     */
    public static final String FORMAT15 = "YYYY-MM-dd HH-mm-ss";

    /***
     * yyyy.MM.dd HH:mm:ss
     */
    public static final String FORMAT8 = "yyyy.MM.dd HH:mm:ss";

    /***
     * yyyy/MM/dd HH:mm:ss
     * */
    public static final String FORMAT9 = "yyyy/MM/dd HH:mm:ss";

    /***
     * yyyy_MM_dd_HH_mm_ss
     */
    public static final String FORMAT10 = "yyyy_MM_dd_HH_mm_ss";

    /**
     * yy-MM-dd
     */
    public static final String FORMAT11 = "yy-MM-dd";

    /**
     * yyyyMMdd
     */
    public static final String FORMAT12 = "yyyyMMdd";

    /***
     * yyyyMMddHHmm
     */
    public static final String FORMAT24 = "yyyyMMddHHmm";

    /***
     * yyyyMMddHHmmss
     */
    public static final String FORMAT13 = "yyyyMMddHHmmss";

    /**
     * yyyyMM
     */
    public static final String FORMAT14 = "yyyyMM";

    /**
     * yyyy-MM
     */
    public static final String FORMAT16 = "yyyy-MM";

    /***
     * yyMMdd
     */
    public static final String FORMAT17 = "yyMMdd";

    /**
     * yyMM
     */
    public static final String FORMAT18 = "yyMM";

    /***
     * MMdd
     */
    public static final String FORMAT19 = "MMdd";


    public static final String FORMAT20 = "yyyy年MM月";

    public static final String FORMAT25 = "yyyy年MM月dd日";
    /***
     * yyyy-MM-dd
     */
    public static final String FORMAT21 = "yyyy-MM";

    /***
     * yyyy-MM-dd
     */
    public static final String FORMAT22 = "yyyy";

    public static final String FORMAT23 = "MM月dd日";

    public static final String FORMAT34 = "yyyyMMddHHmmssSSS";

    public static final DateTimeFormatter FORMATER1 = DateTimeFormatter.ofPattern(FORMAT1);
    public static final DateTimeFormatter FORMATER4 = DateTimeFormatter.ofPattern(FORMAT4);
    public static final DateTimeFormatter FORMATER17 = DateTimeFormatter.ofPattern(FORMAT17);
    public static final DateTimeFormatter FORMATER18 = DateTimeFormatter.ofPattern(FORMAT18);
    public static final DateTimeFormatter FORMATER19 = DateTimeFormatter.ofPattern(FORMAT19);

    public static LocalDate toLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.toLocalDate();
    }

    public static LocalTime toLocalTime(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.toLocalTime();
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static Date getCurrentDate() {

        return new Date(System.currentTimeMillis());
    }

    public static Date getYesterDay() {

        return addDay(new Date(System.currentTimeMillis()), -1);
    }

    public static String getYesterDayString() {

        return parseDateToString(addDay(new Date(System.currentTimeMillis()), 0), FORMAT1);
    }

    /***
     * 得到年月日的路径
     * @return
     */
    public static String getThisYearMonthDay(String dateString) {

        Date date = parseStringToDate(dateString, FORMAT12);

        return DateUtil.getYear(date) + "/" + DateUtil.getMonth(date) + "/" + DateUtil.getDay(date) + "/";
    }

    /**
     * 获取指定日期的指定格式
     *
     * @param format
     * @param days   单签天数
     * @return
     */
    public static String getCurrentDate(String format, Integer days) {

        if (StringUtils.isBlank(format)) {
            format = FORMAT1;
        }
        Calendar c = new GregorianCalendar(1900, 0, -1);
        Date d = c.getTime();
        Date date = DateUtils.addDays(d, days);
        SimpleDateFormat formatter = new SimpleDateFormat(format);

        String currentTime = formatter.format(date);
        if (currentTime.contains("年0")) {
            currentTime = currentTime.replace("年0", "年");
        }
        return currentTime;
    }

    /***
     * 返回当前日期或时间
     *
     * @param format
     * @return
     */
    public static String getCurrentDate(String format) {

        if (StringUtils.isBlank(format)) {
            format = FORMAT1;
        }

        Date date = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat(format);

        String currentTime = formatter.format(date);

        return currentTime;
    }

    /***
     * 返回当前时间
     *
     * @return
     */
    public static String getCurrentTime() {

        String datetime = getCurrentDate(FORMAT7);
        String time = datetime.substring(datetime.indexOf(" ") + 1);

        return time;
    }

    /***
     * 返回当前时间加随机数
     *
     * @return
     */
    public static String getCurrentDateTimeRandom() {

        String datetime = getCurrentDate(DateUtil.FORMAT10) + "_" + Math.random();

        return datetime;
    }

    /***
     * 返回当前时间和日期
     * @return
     */
    public static String getCurrentDateTimeString() {

        return getCurrentDate(DateUtil.FORMAT7);

    }

    /***
     * 返回当前时间和日期,格式yyyyMMddHHmmss
     * @return
     */
    public static String getCurrTime_YYYYMMDDHHMMSS() {

        return getCurrentDate(DateUtil.FORMAT13);

    }

    /***
     * 返回当前日期
     *
     * @return
     */
    public static Date getCurrentDateTime() {

        String datetime = getCurrentDate(FORMAT7);

        return parseStringToDate(datetime, "");
    }

    public static Timestamp getCurrentTimestamp() {

        String datetime = getCurrentDate(FORMAT7);

        return parseStringToTimestamp(datetime, "");
    }

    public static Timestamp getCurrentTimestamp(String format) {

        String datetime = getCurrentDate(format);

        return parseStringToTimestamp(datetime, "");
    }

    /***
     * 日期转换为字符串
     *
     * @param date 日期
     * @param format 格式
     * @return 返回字符型日期
     */
    public static String parseDateToString(Date date, String format) {

        String result = "";
        DateFormat formatter = null;
        try {
            if (date != null) {
                if (StringUtils.isBlank(format)) {
                    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                } else {
                    formatter = new SimpleDateFormat(format);
                }
                result = formatter.format(date);
            }
        } catch (Exception e) {
        }

        return result;
    }

    /**
     * 毫秒转会为 天，时，分，秒格式
     */
    public static String secondToTime(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + "天" + hours + "时" + minutes + "分" + seconds + "秒";
    }

    /***
     * 时间1-时间2的毫秒
     *
     * @param t1
     * @param t2
     * @return
     */
    public static long between(Date t1, Date t2) {

        if ((t1 != null) && (t2 != null)) {
            return t1.getTime() - t2.getTime();
        }

        return 0;
    }

    /***
     * 两个日期date1-date2相减，相差的天数
     *
     * @param date1
     *             日期
     * @param date2
     *             日期
     * @return 返回相减后的日期
     */
    public static int betweenTwoDates(Date date1, Date date2) {

        return (int) ((getMillis(date1) - getMillis(date2)) / (24 * 3600 * 1000));
    }

    /***
     * 两个日期date1-date2相减，相差的天数,不计算秒.只按天算
     *
     * @param date1
     *             日期
     * @param date2
     *             日期
     * @return 返回相减后的日期
     */
    public static int betweenDay(Date date1, Date date2) {
        date1 = parseStringToDate(parseDateToString(date1, FORMAT1), FORMAT1);
        date2 = parseStringToDate(parseDateToString(date2, FORMAT1), FORMAT1);
        return (int) ((getMillis(date1) - getMillis(date2)) / (24 * 3600 * 1000));
    }

    /***
     * 两个日期date1-date2相减，相差的月份
     *
     * @param date1
     *             结束日期
     * @param date2
     *             起始日期
     * @return 返回相减后的月份
     */
    public static int betweenTwoDateMonths(Date date1, Date date2) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(date2);
        end.setTime(date1);
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return month + result;
    }

    public static int betweenTwoDateYears(Date date1, Date date2) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(date2);
        end.setTime(date1);
        int year = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
        return year;
    }

    /***
     * 将字符串转换为日期
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parseStringToDate(String str, String format) {
        DateFormat formatter = null;
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
            if (StringUtils.isBlank(format)) {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            } else {
                formatter = new SimpleDateFormat(format);
            }
            try {
                date = formatter.parse(str);
            } catch (ParseException e) {
                log.error("parseStringToDate转换日期异常:{}.{},{}", e.getMessage(), str, format);
            }
        }
        return date;
    }

    public static Date stringToDate(String str, String format) {
        DateFormat formatter = null;
        Date date = null;
        if (StringUtils.isNotBlank(str)) {

            if (StringUtils.isBlank(format)) {
                if (str.endsWith("月")) {
                    formatter = new SimpleDateFormat("yyyy年MM月");
                } else {
                    formatter = new SimpleDateFormat("yyyy年");
                }
            } else {
                formatter = new SimpleDateFormat(format);
            }

            try {
                date = formatter.parse(str);
            } catch (ParseException e) {
                log.error("stringToDate转换日期异常:{}.{},{}", e.getMessage(), str, format);
            }
        }

        return date;
    }

    /***
     * 返回2007到今年的年份
     *
     * @return
     */
    public static List<Integer> get2007ToThisYear() {

        // 当前时间
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        // 返回的年份
        List<Integer> the2007ToThisYearList = new ArrayList<Integer>();
        // 当前年
        int thisYear = c.get(Calendar.YEAR);

        for (int i = thisYear; i >= 2007; i--) {
            the2007ToThisYearList.add(i);
        }

        return the2007ToThisYearList;
    }

    /***
     *  获取当前月的前几个月份的时间
     * @param months
     * @return
     */
    public static Date getDateBeforeMonths(int months) {

        // 当前时间
        Calendar c = Calendar.getInstance();

        c.add(Calendar.MONTH, -months);

        return c.getTime();
    }

    /***
     * 获取任意几个月份的日期列表
     * @param month
     * @param sort 排序，给大于0则正序排序，反之
     * @param format 时间格式
     * @return
     */
    public static List<String> getDateBeforeMonthsList(int month, Integer sort, String format) {
        List<String> beforeList = new ArrayList<String>();
        for (int i = 0; i < month; i++) {
            beforeList.add(parseDateToString(getDateBeforeMonths(i), format));
        }

        if (sort > 0) {
            List<String> orderByList = new ArrayList<String>();

            for (int j = beforeList.size() - 1; j >= 0; j--) {
                String beforeDate = beforeList.get(j);
                orderByList.add(beforeDate);
            }
            return orderByList;
        }
        return beforeList;
    }


    /***
     * 获取当前日期的前几天的时间
     * @param days
     * @return
     */
    public static Date getDateBeforeDays(int days) {

        // 当前时间
        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, -days);
        return c.getTime();
    }

    /***
     * 获取任意几天的日期列表
     * @param days
     * @return
     */
    public static List<String> getDateBeforeDaysList(int days) {
        List<String> beforeList = new ArrayList<String>();
        for (int i = 0; i < days; i++) {
            beforeList.add(parseDateToString(getDateBeforeDays(i), FORMAT1));
        }

        List<String> orderByList = new ArrayList<String>();

        for (int j = beforeList.size() - 1; j >= 0; j--) {
            String beforeDate = beforeList.get(j);
            orderByList.add(beforeDate);
        }

        return orderByList;
    }

    /***
     * 返回1-12月份
     *
     * @return
     */
    public static List<String> getAllMonth() {

        List<String> theMonthList = new ArrayList<String>();

        for (int i = 1; i < 13; i++) {
            if (i < 10) {
                theMonthList.add("0" + i);
            } else {
                theMonthList.add("" + i);
            }
        }

        return theMonthList;
    }

    /***
     * 返回日期中的年份
     *
     * @param date
     *             日期
     * @return 返回年份
     */
    public static int getYear(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(Calendar.YEAR);
    }

    /***
     * 返回日期中的月份
     *
     * @param date
     *             日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(Calendar.MONTH) + 1;
    }

    /***
     * 返回日期中的日
     *
     * @param date
     *             日期
     * @return 返回日
     */
    public static int getDay(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(Calendar.DAY_OF_MONTH);
    }

    public static String getDayStr() {
        return getCurrentDate("yyyyMMdd");
    }

    public static Date getFirstDateOfMonth(int amount) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);

        return cale.getTime();
    }

    public static Date getLastDateOfMonth(int amount) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);

        return cale.getTime();
    }


    /***
     * 返回日期中的小时
     *
     * @param date
     *             日期
     * @return 返回小时
     */
    public static int getHour(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(Calendar.HOUR_OF_DAY);
    }

    /***
     * 返回日期中的分钟
     *
     * @param date
     *             日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(Calendar.MINUTE);
    }

    /***
     * 返回日期中的秒钟
     *
     * @param date
     *             日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(Calendar.SECOND);
    }

    /***
     * 返回日期代表的毫秒
     *
     * @param date
     *             日期
     * @return 返回毫秒
     */
    public static long getMillis(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.getTimeInMillis();
    }

    /***
     * 两个日期比较
     *
     * @param d1,d2
     *             日期
     * @return d1>d2 返回true
     */
    public static Boolean gre(Date d1, Date d2) {
        long difMillis = getMillis(d1) - getMillis(d2);
        if (difMillis > 0) {
            return true;
        } else {
            return false;
        }
    }

    /***
     * 两个日期比较
     *
     * @param
     * @return d1<d2 ,   返   回   t   r   u   e  */
    public static Boolean less(Date d1, Date d2) {
        long difMillis = getMillis(d1) - getMillis(d2);
        if (difMillis < 0) {
            return true;
        } else {
            return false;
        }
    }

    /***
     * 返回当前日期代表的毫秒
     */
    public static long getCurrentTimeMillis() {

        return System.currentTimeMillis();
    }

    /**
     * 增加月
     */
    public static Date addMonth(Date date, int month) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }

    /**
     * 增加年
     */
    public static Date addYear(Date date, int year) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, year);

        return c.getTime();
    }

    /***
     * 日期加天数,可以向前加，向后加
     *
     * @param date
     *             日期
     * @param day
     *             天数
     * @return 返回相加后的日期
     */
    public static Date addDay(Date date, int day) {

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);

        return c.getTime();
    }

    /***
     * 日期加秒数,可以向前加，向后加
     *
     * @param date
     *             日期
     * @param second
     *             秒数
     * @return 返回相加后的日期
     */
    public static Date addSecond(Date date, long second) {

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + second * 1000);

        return c.getTime();
    }


    /**
     * @param d1 需要比较的时间
     * @param d2 开始时间
     * @param d3 结算时间
     * @return Boolean
     */
    public static Boolean dateBetween(Date d1, Date d2, Date d3) {
        Boolean ret = false;
        if (d2 == null || d3 == null) {
            return ret;
        }
        if ((d1.after(d2) || d1.compareTo(d2) == 0) && (d1.before(d3) || d1.compareTo(d3) == 0)) {
            ret = true;
        }
        return ret;
    }

    /***
     * 根据一个日期，返回是星期几
     *
     * @param sdate
     * @return
     */
    public static String getWeekByDate(String sdate) {

        // 再转换为时间
        Date date = parseStringToDate(sdate, "");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DAY_OF_WEEK);
        // day中存的就是星期几了，其范围 1~7
        // 1=星期日 7=星期六，其他类推
        return day + "";
    }

    /**
     * 根据一个日期，返回是星期几
     * 1=星期日 7=星期六，其他类推
     *
     * @param date
     * @return
     */
    public static int getWeekByDate(Date date) {

        // 再转换为时间
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // day中存的就是星期几了，其范围 1~7
        // 1=星期日 7=星期六，其他类推
        return c.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 日期为一年中的第几周
     *
     * @return
     */
    public static String getWeekNum(Date date) {

        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.setTime(date);
        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));

        return week;

    }

    public static java.sql.Date parseUtilDateToSqlDate(Date date) {

        if (date != null) {
            return new java.sql.Date(date.getTime());
        } else {
            return null;
        }
    }

    public static java.sql.Date parseStringToSqlDate(String dateStr, String format) {

        Date date = null;
        if (StringUtils.isBlank(format)) {
            date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
        } else {
            date = parseStringToDate(dateStr, format);
        }

        return parseUtilDateToSqlDate(date);
    }

    public static Timestamp parseUtilDateToTimestamp(Date date, String format) {

        return parseStringToTimestamp(parseDateToString(date, format), format);
    }

    public static Date parseTimestampToUtilDate(Timestamp date, String format) {

        return parseStringToDate(parseDateToString(date, format), format);
    }

    public static Timestamp parseStringToTimestamp(String dateStr, String format) {

        if (StringUtils.isBlank(dateStr)) {
            return null;
        }

        Date date = null;
        if (StringUtils.isBlank(format)) {
            date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
        } else {
            date = parseStringToDate(dateStr, format);
        }

        if (date != null) {
            long t = date.getTime();
            return new Timestamp(t);
        } else {
            return null;
        }
    }

    /**
     * 获取时间2099-12-31，23:59:59
     *
     * @return
     */
    public static Timestamp getMaxTimestamp() {

        return DateUtil.parseStringToTimestamp("2099-12-31 23:59:59", DateUtil.FORMAT7);
    }

    /**
     * 返回日期中的年月日，格式化成yyyyMMdd
     *
     * @param date
     * @return
     */
    public static String getYearMonthDay(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT12);

        String currentTime = formatter.format(date);

        return currentTime;
    }

    public static String getYearMonth(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT20);

        String currentTime = formatter.format(date);

        return currentTime;
    }

    public static String getMonthDay(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT23);

        String currentTime = formatter.format(date);

        return currentTime;
    }

    /**
     * 取得指定月份的第一天
     *
     * @param strdate String
     * @return String
     */
    public static String getMonthBegin(String strdate) {

        return parseDateToString(parseStringToDate(strdate, "yyyy-MM"), "");
    }

    /**
     * 取得指定月份的最后一天
     *
     * @param strdate String
     * @return String
     */
    public static String getMonthEnd(String strdate) {

        Date date = parseStringToDate(getMonthBegin(strdate), "");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);

        return parseDateToString(calendar.getTime(), "");
    }

    public static String getPreviousMonthBegin() {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        Date date = new Date(cal.getTimeInMillis());

        return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT1);
    }

    public static String getPreviousMonthEnd() {

        Date date = parseStringToDate(getPreviousMonthBegin(), FORMAT1);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);

        return parseDateToString(calendar.getTime(), FORMAT1);
    }

    /**
     * 上个月
     *
     * @return
     */
    public static String getPreviousMonth() {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        Date date = new Date(cal.getTimeInMillis());

        return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT14);
    }

    /**
     * 上两个月
     *
     * @return
     */
    public static String getPreviousTwoMonth() {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 2);
        Date date = new Date(cal.getTimeInMillis());

        return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT14);
    }


    /**
     * 当日，周，月第一天第一秒 2016-01-01 00:00:00
     */
    public static Date getFirstDayByType(int calendarType) {
        Date date = new Date();
        return getFirstDayByType(date, calendarType);
    }

    /**
     * 当日，周，月第一天第一秒 2016-01-01 00:00:00
     */
    public static Date getFirstDayByType(Date date, int calendarType) {
        if (calendarType == Calendar.DAY_OF_WEEK) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.DAY_OF_WEEK, 2);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            date = c.getTime();
            return date;
        }
        return DateUtils.truncate(date, calendarType);
    }

    /**
     * 当日，周，月最后一天最后一秒 2016-01-11 23:59:59
     */
    public static Date getLastDayByType(int calendarType) {
        Date date = new Date();
        return getLastDayByType(date, calendarType);
    }

    /**
     * 当日，周，月最后一天最后一秒 2016-01-11 23:59:59
     */
    public static Date getLastDayByType(Date date, int calendarType) {
        if (calendarType == Calendar.DAY_OF_WEEK) {
            Calendar c = Calendar.getInstance();
            c = Calendar.getInstance();
            c.set(Calendar.DAY_OF_WEEK, 7);
            c.add(Calendar.DATE, 1);
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            date = c.getTime();
            return date;
        }
        return DateUtils.addMilliseconds(DateUtils.ceiling(date, calendarType), -1);
    }

    /**
     * 获取某月最大天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMaxDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        return lastDay;
    }

    /**
     * 判断两个日期是否在同一周
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDates(Date date1, Date date2) {

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if ((1 == subYear) && (11 == cal2.get(Calendar.MONTH))) {
            // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if ((-1 == subYear) && (11 == cal1.get(Calendar.MONTH))) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        }
        return false;
    }

    public static String addOneDay(String stopTime) {
        String finishTime = stopTime;
        Date finishDate = null;
        if (stopTime != null && !"".equals(stopTime)) {
            if (stopTime.length() < 19) {
                finishTime = stopTime.substring(0, 10) + " 00:00:00";
            }
            finishDate = DateUtil.parseStringToDate(finishTime, DateUtil.FORMAT7);
            finishDate = DateUtil.addDay(finishDate, 1);
            return DateUtil.parseDateToString(finishDate, DateUtil.FORMAT7);
        } else {
            return null;
        }
    }


    /**
     * 给date减1个月
     *
     * @param date
     * @return
     */
    public static Date subOneMonth(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

    /**
     * 给date加1天
     *
     * @param date
     * @return
     */
    public static Date addOneDay(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 给date加1个月
     *
     * @param date
     * @return
     */
    public static Date addOneMonth(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /***
     * 返回中文星期几(如星期一，星期二。。。。)
     * @param dt
     * @return
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /*** 
     * 根据开始时间和结束时间返回时间段内的时间集合 
     *
     * @param beginDate
     * @param endDate
     * @return List
     * @throws ParseException
     */
    public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = sdf.parse(beginDate);
        Date dEnd = sdf.parse(endDate);

        List<String> lDate = new ArrayList<String>();
        // 把开始时间加入集合
        lDate.add(parseDateToString(dBegin, DateUtil.FORMAT1));
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间  
        cal.setTime(dBegin);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量  
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后  
            if (dEnd.after(cal.getTime())) {
                lDate.add(parseDateToString(cal.getTime(), DateUtil.FORMAT1));
            } else {
                break;
            }
        }
        // 把结束时间加入集合
        lDate.add(parseDateToString(dEnd, DateUtil.FORMAT1));
        return lDate;
    }

    /***
     * 根据开始时间和结束时间返回时间段内的“年-月”集合
     * @throws ParseException
     */
    public static List<String> getFormatListBetweenTwoDate(Date beginDate, Date endDate, int calendarConstant, String format) throws ParseException {
        List<String> dateStrList = new ArrayList<String>();
        // 把开始时间加入集合
        dateStrList.add(parseDateToString(beginDate, format));
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(calendarConstant, 1);
            // 测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                dateStrList.add(parseDateToString(cal.getTime(), format));
            } else {
                break;
            }
        }
        // 如果不包含结束时间，把结束时间加入集合
        String s = parseDateToString(endDate, format);
        if (!dateStrList.contains(s)) dateStrList.add(parseDateToString(endDate, format));
        return dateStrList;
    }

    /**
     * 设置查询的开始日期的时分秒
     *
     * @param startDate
     * @return
     */
    public static Date setQueryStartTime(Date startDate) {
        if (startDate == null) {
            return null;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startDate);
        // 将时分秒,毫秒域清零
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        return cal1.getTime();
    }

    /**
     * 查询的结束日期的时分秒
     *
     * @param endDate
     * @return
     */
    public static Date setQueryEndTime(Date endDate) {
        if (endDate == null) {
            return null;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(endDate);
        // 将时分秒,毫秒域清零
        cal1.set(Calendar.HOUR_OF_DAY, 23);
        cal1.set(Calendar.MINUTE, 59);
        cal1.set(Calendar.SECOND, 59);
        cal1.set(Calendar.MILLISECOND, 59);
        return cal1.getTime();
    }

    public static Date truncDate(Date date) {
        if (date == null) {
            return null;
        }
        return parseStringToDate(parseDateToString(date, FORMAT12), FORMAT12);
    }

    public static String getDateStr(Date date, String formatRegex) {
        if (date == null) {
            return null;
        }
        return parseDateToString(date, formatRegex);
    }

    /**
     * 获取星期
     *
     * @param date
     * @return
     */
    public static String getWeekDay(Date date) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }

        return WEEK_DAYS[w];
    }

    /**
     * 获取当前日期月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayByDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());

        return parseStringToDate(first, FORMAT1);
    }

    /**
     * 获取当前日期的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayByDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(c.getTime());

        return parseStringToDate(last, FORMAT1);
    }

    /**
     * 获取当前日期,带时分秒 00:00:00
     *
     * @param date
     * @return
     */
    public static Date getCurrentDayHmsBegin(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        String last = format.format(c.getTime());

        return parseStringToDate(last, FORMAT7);
    }

    /**
     * 获取当前日期的最后一天,带时分秒 23:59:59
     *
     * @param date
     * @return
     */
    public static Date getCurrentDayHmsEnd(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        String last = format.format(c.getTime());

        return parseStringToDate(last, FORMAT7);
    }

    /**
     * 获取当前日期月的第一天,带时分秒 00:00:00
     *
     * @param date
     * @return
     */
    public static Date getFirstDayByDateHms(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        String last = format.format(c.getTime());

        return parseStringToDate(last, FORMAT7);
    }

    /**
     * 获取当前日期的最后一天,带时分秒 23:59:59
     *
     * @param date
     * @return
     */
    public static Date getLastDayByDateHms(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        String last = format.format(c.getTime());

        return parseStringToDate(last, FORMAT7);
    }


    /**
     * 获取当年的第一天
     *
     * @param date
     * @return
     */
    public static Date getCurrYearFirst(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentYear = c.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     *
     * @param date
     * @return
     */
    public static Date getCurrYearLast(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentYear = c.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 获取date的季度第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfQuarter(Date date) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(date);
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);
        return startCalendar.getTime();
    }

    /**
     * 获取date的季度最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfQuarter(Date date) {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(date);
        endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return endCalendar.getTime();
    }

    private static void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private static void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }

    /**
     * 校验String类型的date格式是否正确
     *
     * @param sDate 校验日期
     * @param fmt   目标格式
     * @return
     */
    public static boolean isLegalDate(String sDate, String fmt) {
        if (null == fmt || "".equals(fmt)) {
            return false;
        }
        int legalLen = fmt.length();
        if ((sDate == null) || (sDate.length() != legalLen)) {
            return false;
        }

        DateFormat formatter = new SimpleDateFormat(fmt);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断字符是否是日期格式
     *
     * @param str
     * @return
     */
    public static boolean isDate(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Date date = parseStringToDate(str);
        return date != null;
    }

    /***
     * 将字符串转换为日期
     *
     * @param str
     * @return
     */
    public static Date parseStringToDate(String str) {
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
            String format = FORMAT7;
            if (str.length() == 10) {
                format = FORMAT1;
            }
            DateFormat formatter = new SimpleDateFormat(format);
            try {
                date = formatter.parse(str);
            } catch (ParseException e) {
                // 不打印异常
                // log.error("parseStringToDate转换日期异常:{}.{}", e.getMessage(), str);
            }
        }
        return date;
    }

    /**
     * 判断当前日期是否在生效失效日期之间
     *
     * @param validDate   生效日期
     * @param invalidDate 失效日期
     * @return
     */
    public static boolean isValidDateThis(Date validDate, Date invalidDate) {
        return isValidDate(new Date(), validDate, invalidDate);
    }

    /**
     * 判断日期是否在生效失效日期之间
     *
     * @param date        比较日期
     * @param validDate   生效日期
     * @param invalidDate 失效日期
     * @return
     */
    public static boolean isValidDate(Date date, Date validDate, Date invalidDate) {
        if (date == null) {
            return false;
        }
        if (validDate == null || invalidDate == null) {
            // 生效日期或失效日期有一个为空 返回 false
            return false;
        }
        return betweenTwoDates(date, validDate) >= 0 && betweenTwoDates(date, invalidDate) <= 0;
    }

    /**
     * 当前季度的开始时间
     *
     * @return
     */
    public static Date getCurrentQuarterFirstDate() {
        return getQuarterFirstDate(new Date());
    }

    /**
     * 指定日期季度的第一天
     */
    public static Date getQuarterFirstDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        if (currentMonth >= 1 && currentMonth <= 3) {
            c.set(Calendar.MONTH, 0);
        } else if (currentMonth >= 4 && currentMonth <= 6) {
            c.set(Calendar.MONTH, 3);
        } else if (currentMonth >= 7 && currentMonth <= 9) {
            c.set(Calendar.MONTH, 6);
        } else if (currentMonth >= 10 && currentMonth <= 12) {
            c.set(Calendar.MONTH, 9);
        }
        c.set(Calendar.DATE, 1);
        now = parseStringToDate(parseDateToString(c.getTime(), FORMAT1) + " 00:00:00", "");

        return now;
    }

    /**
     * 指定日期季度
     */
    public static int getQuarter(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        if (currentMonth <= 3) {
            return 1;
        } else if (currentMonth <= 6) {
            return 2;
        } else if (currentMonth <= 9) {
            return 3;
        } else if (currentMonth <= 12) {
            return 4;
        } else {
            return -1;
        }
    }

    /**
     * 日期为一年中的第几周
     *
     * @return
     */
    public static int getWeek(Date date) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.setTime(date);
        // c.setMinimalDaysInFirstWeek(4);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    public static Date addQuarter(Date date, int quarter) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 增加一个季度
        cal.add(Calendar.MONTH, 3 * quarter);
        return cal.getTime();
    }

    public static Date addWeek(Date date, int week) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 增加一个星期(7天)
        cal.add(Calendar.DAY_OF_WEEK, 7 * week);
        return cal.getTime();
    }

    /**
     * 获取当前日期 没有时分秒
     */
    public static Date getCDate() {
        return parseStringToDate(parseDateToString(new Date(), FORMAT1), FORMAT1);
    }

    public static Date getMaxDate(Collection<Date> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return null;
        }
        Date maxDate = null;
        for (Date date : collection) {
            if (date == null) {
                continue;
            }
            if (maxDate == null) {
                maxDate = date;
            }
            if (date.compareTo(maxDate) > 0) {
                maxDate = date;
            }
        }
        return maxDate;
    }

    /**
     * 根据秒数获取时间串
     *
     * @param second (eg: 100s)
     * @return (eg : 00 : 01 : 40)
     */
    public static String getTimeStrBySecond(long second) {

        long day = second / 60 / 60 / 24;
        long hour = second / 60 / 60 % 24;
        long minute = second / 60 % 60;
        long seconds = second % 60;

        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day).append("天");
        }
        if (hour != 0) {
            sb.append(hour).append("小时");
        }
        if (minute != 0) {
            sb.append(minute).append("分");
        }
        if (seconds != 0) {
            sb.append(seconds).append("秒");
        }
        return sb.toString();
    }

    public static int getExpire(int expire, String unit) {
        int e = expire;
        if ("seconds".equalsIgnoreCase(unit)) {
            e = expire;
        }
        if ("minute".equalsIgnoreCase(unit)) {
            e = expire * 60;
        }
        if ("hours".equalsIgnoreCase(unit)) {
            e = expire * 60 * 60;
        }
        if ("days".equalsIgnoreCase(unit)) {
            e = (expire * 60 * 60) * 24;
        }
        return e;
    }

    /***
     * 两个日期比较
     *
     * @param
     * @return d1<= d2                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                               返                               回                               t                               r                               u                               e                */
    public static Boolean lessEqual(Date d1, Date d2) {
        long difMillis = getMillis(d1) - getMillis(d2);
        if (difMillis <= 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 获得当天最小时间
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
                ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获得当天最大时间
     */
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
                ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }
}